/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.wwm.DAO.impl;

import edu.fjut.se1603.wwm.DAO.BaseDao;
import edu.fjut.se1603.wwm.DAO.PetStoreDao;
import edu.fjut.se1603.wwm.entity.PetStore;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.util.List;

/**
 *
 * @author Daydream
 */
public class PetStoreDaoImpl extends BaseDao implements PetStoreDao {

    @Override
    public int add(PetStore store) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("insert into petstore(ID,NAME,PASSWORD,BALANCE) values(?,?,?,?)");
            pst.setInt(1, store.getId());
            pst.setString(2, store.getName());
            pst.setString(3, store.getPassword());
            pst.setInt(4, store.getBalance());
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetStoreDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public PetStore getById(int id) {
        PetStore store = new PetStore();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from petstore where ID=" + id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                store.setId(id);
                store.setName(rs.getString("NAME"));
                store.setPassword(rs.getString("PASSWORD"));
                store.setBalance(rs.getInt("BALANCE"));
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetStoreDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return store;
    }

    @Override
    public List<PetStore> getList() {
        List<PetStore> stores = new ArrayList<PetStore>();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from petstore");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                PetStore store = new PetStore();
                store.setId(rs.getInt("ID"));
                store.setName(rs.getString("NAME"));
                store.setPassword(rs.getString("PASSWORD"));
                store.setBalance(rs.getInt("BALANCE"));
                stores.add(store);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetStoreDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return stores;
    }

    @Override
    public int delete(Integer id) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("delete from petstore where ID=" + id);
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetStoreDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public int change(Integer storeID, Integer balance, int type) {
        int r = 0;
        try {
            PetStore store = this.getById(storeID);
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("update petstore set BALANCE=? where ID=" + storeID);
            if (type == 1) {
                pst.setInt(1, (store.getBalance() + balance));
            } else if (type == 0) {
                pst.setInt(1, (store.getBalance() - balance));
            }
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetStoreDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

}
